//参数经理类
const weexFunction = weex.requireModule("WeexFunction");
import weexHttp from "@/_js/https"; ;

import modal from "@/_js/modal";
import API from "@/_js/api";
var I18N = {};
I18N.en = {
  "终止成功":"Successful termination",
  "本地未找到 &{1} API, 或该API已过期,无法完成操作":"The local & {1} API was not found, or it has expired and cannot complete the operation",
};
I18N.ja = {
  "终止成功":"终止成功",
  "本地未找到 &{1} API, 或该API已过期,无法完成操作":"本地未找到 &{1} API, 或该API已过期,无法完成操作",
};

I18N.ko = {
  "终止成功":"终止成功",
  "本地未找到 &{1} API, 或该API已过期,无法完成操作":"本地未找到 &{1} API, 或该API已过期,无法完成操作",
};

var language = weex.config.language;
// var language = "en";
var msg = I18N[language];
if(!msg){
  msg = {};
}
var T = function(text){
    return msg[text] || text;
};
export default {
  
  /** 校验参数
   * 
   * @param {JSON} param 参数
   * @param {Array} keys 参数关键字数组
   */
  checkParam(param, keys){
    for (let index = 0; index < keys.length; index++) {
      var key = keys[index];
      if (param[key] == null) {
        modal.hideLoad();
        modal.toast('缺失：'+key);
        return true;
      }
    }
    return false;
  },

  /** 校验API
   * 
   * @param {String} apiId apiId
   * @param {String} apiName Ap名称
   */
  checkApi(apiId, apiName, success=()=>{}){
    
    API.getApi(apiId, 'YES', (api)=>{
      if (apiId.length > 0 && api == null) {
        modal.toast(T('本地未找到 &{1} API, 或该API已过期,无法完成操作').replace('&(1)',apiName));
      } else {
        success(api);
      }
    });
  },

  /** 获取API参数
   * 
   * @param {JSON} api api
   * @param {Function} success 成功回调
   */
  getApiParam(api, success=(apiMap)=>{}){

    if (api == null) { 
      success({});
      return;
    }
    
    if (api.apiId == null || api.apiId == '') {
      var apiMap = {'isMimic':true, 'apiName':api.exchangeName, 'apiId':'', 'api':''};
      success(apiMap);
      return;
    }
    this.httpPublicKey(api.apiId, (key)=>{
      API.rsaApi(api, key, (rsa)=>{
        var apiMap = {'isMimic':false, 'apiName':api.apiName, 'apiId':api.apiId, 'api':rsa};
        success(apiMap);
      });
    });
  },

  /** 获取执行配置
   * 
   * @param {JSON} param 参数
   * @param {JSON} apiMap api参数
   */
  getExecuteConfig(param, apiMap){
    
    //1、交易对数据
    var pairMap = {};
    if (param.pairMap) {
      pairMap = param.pairMap;
      var pairKeys = ['singal', 'last', 'usdPrice', 'baseCoinUsdPrice' ,'increase'];
      for (let index = 0; index < pairKeys.length; index++) {
        var key = pairKeys[index];
        delete pairMap[key];
      }
      pairMap['exchange'] = pairMap.exchangeName;
    }

    //2、投入数据
    var investMap = {};
    if (param.buyFee) {
      investMap['buyFee'] = param.buyFee;
      investMap['soldFee'] = param.buyFee;
    }
    if (param.investCoin) investMap['investCoin'] = param.investCoin;
    if (param.investCoinnNum) {
      investMap['investCoinnNum'] = param.investCoinnNum;
      investMap['counterCoinAmount'] = param.investType ? param.investCoinnNum : '0';
      investMap['baseCoinAmount'] = param.investType ?  '0' : param.investCoinnNum;
    }
    
    /** 3、完整执行配置-交易对：API、交易对、投入信息
     *  参数数组
     *  其他设置
     *  多币对
     */
    var executeConfig = param.executeConfig;
    var tradePair = executeConfig['tradePairs'][0];
    tradePair = Object.assign(tradePair, apiMap);
    tradePair = Object.assign(tradePair, pairMap);
    tradePair = Object.assign(tradePair, investMap);
    if (param.reserves) executeConfig['reserves'] =  param.reserves;


    if (param.signal) executeConfig['signal'] = param.signal;
    if (param.setMap) executeConfig = Object.assign(executeConfig, param.setMap);
    if (param.pairs) {
      var newPairs = [{'counterCoin':pairMap.counterCoin, 'baseCoin':pairMap.baseCoin}];
      for (let index = 0; index < param.pairs.length; index++) {
        var newPair = param.pairs[index];
        newPairs.push({'counterCoin':newPair.counterCoin, 'baseCoin':newPair.baseCoin});
      }
      executeConfig['pairs'] =  newPairs; 
    }

    return executeConfig;
  },

  /******************************************* http*/
  /** HTTP 获取执行服务器公钥
   * 
   * @param {String} apiId 获取服务器公钥
   * @param {Function} success 成功回调
   */
  httpPublicKey(apiId, success=(key)=>{}){

    if (apiId == null || apiId.length == 0) {
      success('');
      return;
    }

    var param = {serverType:'strategy'};
    weexHttp.post("assetsV2/getPublicKey",param,true,(data)=>{
      
      if(data.status == 200){
        var key = data.data.publicKey;
        success(key);
      } else {
        modal.toast(data.msg);
      }
    });
  },
}
  